//
//  ReadyScoreView.m
//  NewGame
//
//  Created by wwj on 15/10/12.
//  Copyright © 2015年 wwj. All rights reserved.
//

#import "ReadyScoreView.h"
#import "StageRecordModel.h"
#import "StageModel.h"

@interface ReadyScoreView ()
@property (weak ,nonatomic) IBOutlet UIImageView *scoreHint;
@property (nonatomic, strong) IBOutletCollection(UILabel) NSArray *labelArray;
@end

@implementation ReadyScoreView

-(void)setStageModel:(StageModel *)stageModel{
    _stageModel = stageModel;
    
    if (!_stageModel.recordModel.score) {
        self.hidden = YES;
        return;
    }
    
    //设置动画
    [self addAnimation];
    
    CGFloat scoreMax = _stageModel.max;
    CGFloat scoreMin = _stageModel.min;
    
    CGFloat scoreMarge = (scoreMax - scoreMin)/5;
    
    for (int index=0; index<_labelArray.count; index++) {
        CGFloat score = scoreMax - scoreMarge * index;
        UILabel *label = _labelArray[index];
        label.text = [NSString stringWithFormat:_stageModel.format, score];
    }
    
    //设置分数
    [self setScoreWithMarge:scoreMarge];
   
}

-(void)addAnimation{
    self.transform = CGAffineTransformMakeTranslation(0, 400);
    [UIView animateWithDuration:0.3 animations:^{
        self.transform = CGAffineTransformIdentity;
    }completion:^(BOOL finished) {
        CAKeyframeAnimation *hidden = [CAKeyframeAnimation animationWithKeyPath:@"hidden"];
        hidden.values = @[@0, @1, @0];
        hidden.repeatCount = 3;
        [_scoreHint.layer addAnimation:hidden forKey:nil];
    }];
}

-(void)setScoreWithMarge:(CGFloat)scoreMarge{
    
    CGPoint center = _scoreHint.center;
    CGFloat preScoreMarge = center.x/6/scoreMarge;
    
    CGFloat delta = _stageModel.recordModel.score - _stageModel.min;
    
    CGFloat transX = delta * preScoreMarge;
    center.x-= transX;
    
    if (center.x<=0) {
        center.x = 16;
    }
    
    _scoreHint.center = center;
}

@end
